package com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.controller;

import com.alibaba.fastjson.JSONObject;

import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.SimEarthquake;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.queryParams.EarEveManageQuaParams;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.async.service.AsyncService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.vo.EarEveManageVo;
import com.css.fxfzxczh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/earEveManage")
@Slf4j
public class EarEveManageController {
    @Resource
    EarEveManageService earEveManageService;


    @Resource
    AsyncService asyncService;

    /**
     * 分页列表
     *
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(EarEveManageQuaParams queParams, HttpServletRequest request) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = earEveManageService.getPage(queParams, curPage, pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 列表
     *
     * @return
     */
    @GetMapping("/getList")
    public RestResponse getList() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<EarEveManage> list = earEveManageService.getList();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取所有地震事件
     */
    @GetMapping("/getAllEarEveManage")
    public RestResponse getAllEarEveManage() {
        RestResponse restResponse = null;
        try {
            List<EarEveManage> earEveManages = earEveManageService.getAllEarEveManage();
            restResponse = RestResponse.succeed(earEveManages);
            restResponse.setMessage("查询成功");
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 地震信息分页列表
     *
     * @param magnitude
     * @param request
     * @return
     */
    @GetMapping("getEarInfo")
    public RestResponse getEarInfo(HttpServletRequest request,String magnitude,String dataSource) {
        RestResponse restResponse=null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map=earEveManageService.getEarInfo(curPage,pageSize,magnitude,dataSource);
            restResponse= RestResponse.succeed(map);
        }catch (Exception e){
            String errormessage="查看失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;

    }

    /**
     * 模拟地震新增接口
     *
     * @param simEarthquake
     * @return
     */
    @PostMapping("simEarSave")
    public RestResponse simEarSave(@RequestBody SimEarthquake simEarthquake) {
        RestResponse response = null;
        try {
            earEveManageService.simEarSave(simEarthquake);
            response = RestResponse.succeed();
            response.setMessage("保存成功");
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 模拟地震删除接口
     *
     * @param id
     * @return
     */
    @GetMapping("simEarDelete/{id}")
    public RestResponse simEarDelete(@PathVariable("id") String id) {
        RestResponse response = null;
        try {
            earEveManageService.simEarDelete(id);
            response = RestResponse.succeed();
            response.setMessage("删除成功");
        } catch (Exception e) {
            String errormessage = "删除失败";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 模拟地震修改接口
     *
     * @param simEarthquake
     * @return
     */
    @PostMapping("simEarUpdate")
    public RestResponse simEarUpdate(@RequestBody SimEarthquake simEarthquake) {
        RestResponse response = null;
        try {
            earEveManageService.simEarUpdate(simEarthquake);
            response = RestResponse.succeed();
            response.setMessage("修改成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public RestResponse delete(String id) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            earEveManageService.delete(id);
            //超图刷新图层
            asyncService.asyncRefresh("xczh_seismic_event_management");
            jsonObject.put("message", "删除成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "删除失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改
     *
     * @param earEveManage
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody EarEveManage earEveManage) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            earEveManageService.update(earEveManage);
            jsonObject.put("message", "修改成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 保存
     *
     * @param earEveManage
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody EarEveManage earEveManage) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            earEveManageService.save(earEveManage);
            //使用异步任务 超图刷新图层
            asyncService.asyncRefresh("xczh_seismic_event_management");
            jsonObject.put("message", "保存成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "保存失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 设为当前事件
     *
     * @param id
     * @return
     */
    @PostMapping("/setCurEvent/{id}")
    public RestResponse setCurrentEvent(@PathVariable("id")String id) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            earEveManageService.setCurEvent(id);
            jsonObject.put("message", "设置成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "设置失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     *
     *
     * @param params
     * @return
     */
    @GetMapping("/getEventName")
    public RestResponse getEventName(EarEveManageQuaParams params) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            Map<String,Object> name=earEveManageService.getEventName(params);
            response = RestResponse.succeed(name);
        } catch (Exception e) {
            String errorMessage = "生成失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try {
            EarEveManage earEveManage=earEveManageService.findById(id);
            response = RestResponse.succeed(earEveManage);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取当前事件
     *
     * @return
     */
    @GetMapping("/getCurrentEvent")
    public RestResponse getCurrentEvent() {
        RestResponse response = null;
        try {
            EarEveManage earEveManage=earEveManageService.getCurrentEvent();
            response = RestResponse.succeed(earEveManage);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 转换
     *
     * @param earEveManageVo
     * @return
     */
    @PostMapping("/convert")
    public RestResponse convert(@RequestBody EarEveManageVo earEveManageVo) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            earEveManageService.convert(earEveManageVo);
            //超图刷新图层
            asyncService.asyncRefresh("xczh_seismic_event_management");
            jsonObject.put("message", "转换成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "转换失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * app列表
     * @return
     */
    @GetMapping("/getAppPage")
    public RestResponse getAppPage() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {

            List<EarEveManageVo> list = earEveManageService.getAppPage();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 设为当前事件
     *
     * @param id
     * @return
     */
    @GetMapping("/setAppCurEvent")
    public RestResponse setAppCurEvent(String id) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            earEveManageService.setCurEvent(id);
            jsonObject.put("message", "设置成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "设置失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 地震事件结束校验
     *
     * @param id
     * @return
     */
    @PostMapping("/endSeismicEventCheck/{id}")
    public RestResponse endSeismicEventCheck(@PathVariable("id")String id) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String str=earEveManageService.endSeismicEventCheck(id);
            response = RestResponse.succeed(str);
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 地震事件结束
     *
     * @param id
     * @return
     */
    @PostMapping("/endSeismicEvent/{id}")
    public RestResponse endSeismicEvent(@PathVariable("id")String id) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            earEveManageService.endSeismicEvent(id);
            response = RestResponse.succeed();
        } catch (Exception e) {
            String errorMessage = "结束失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
